home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Internet & Communication / IOXWebcamX-1.1.dmg / IOXWebcamX-1.1.pkg / Contents / Resources / UninstallerTools.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2005-05-05  |  11.1 KB  |  536 lines

  1. #!/bin/sh -x
  2.  
  3. # TODO - allow combinations of remove styles with show styles
  4.  
  5. # ----------------------------------------------------------------------------
  6. #
  7. #
  8. #     REMOVE_BIN=$1
  9. #   REMOVE_ACTION=$2
  10. #   REMOVE_VOL=$3
  11. #    REMOVE_SHOW_ONLY=$4
  12. #
  13. function defineRemoveArgs()
  14. {
  15.     if [ -z "$1" ]
  16.     then
  17.         echo "No bin dir specified"
  18.         exit 1
  19.     fi
  20.     
  21.     REMOVE_BIN="$1"
  22.     REMOVE_ACTION="$2"
  23.     REMOVE_VOL="$3"
  24.     REMOVE_SHOW_ONLY="$4"
  25.     
  26.     if [ -z "${REMOVE_ACTION}" ]
  27.     then
  28.         REMOVE_ACTION="trash"
  29.     fi
  30. }
  31.  
  32. # ----------------------------------------------------------------------------
  33. function showRemoveArgs()
  34. {
  35.     echo "    REMOVE_BIN=${REMOVE_BIN}"                    > /dev/stderr
  36.     echo "    REMOVE_ACTION=${REMOVE_ACTION}"            > /dev/stderr
  37.     echo "    REMOVE_VOL=${REMOVE_VOL}"                    > /dev/stderr
  38.     echo "    REMOVE_SHOW_ONLY=${REMOVE_SHOW_ONLY}"        > /dev/stderr
  39.     echo "    " > /dev/stderr
  40.  
  41. }
  42.  
  43. # ----------------------------------------------------------------------------
  44.  
  45. function bailOnError()
  46. {
  47.     if test "$1" -ne 0; then
  48.         echo "$2" > /dev/stderr
  49.         exit 1
  50.     fi
  51. }
  52.  
  53. # ----------------------------------------------------------------------------
  54.  
  55. function validateTool()
  56. {
  57.     if ! [ -z "$1" ]
  58.     then
  59.         if ! [ -x "$1" ]
  60.         then
  61.             echo "$1 is not executable"
  62.             exit 1
  63.         fi
  64.     fi
  65. }
  66.  
  67. # ----------------------------------------------------------------------------
  68. #
  69. # Take $1 $2 and $3 and convert to
  70. #     PACKAGE_NAME, VARIANT, VOLUME, and INSTALLER_TOOLS_DIR
  71. #    $1 is expected to be the full path to the installer .pkg
  72. #
  73. #
  74.  
  75. function locateTools()
  76. {
  77.     if [ -z "${REMOVE_BIN}" ]
  78.     then
  79.         echo "No removal bin defined" > /dev/stderr
  80.         exit 1
  81.     fi
  82.  
  83.     # Trash Tool
  84.     TRASH_TOOL="${REMOVE_BIN}/MoveToTrash"
  85.     if ! [ -x "${TRASH_TOOL}" ]
  86.     then
  87.         TRASH_TOOL=""
  88.     fi
  89.     
  90.     # Locate MvMac if available, look in Developer/Tools and REMOVE_BIN
  91.     MVMAC="/Developer/Tools/MvMac"
  92.     if ! [ -x "${MVMAC}" ]
  93.     then
  94.         MVMAC="${REMOVE_BIN}/MvMac"
  95.     fi
  96.     if ! [ -x "${MVMAC}" ]
  97.     then
  98.         MVMAC=""
  99.     fi
  100.     
  101.     # If no trash tool, the action is remove
  102.     
  103.     if [[ -z "${TRASH_TOOL}" && -z "${MVMAC}" && "${REMOVE_ACTION}" == "trash" ]]
  104.     then
  105.         REMOVE_ACTION="remove"
  106.     fi
  107.     
  108.     # If 10.2 or earlier, the action is remove, Trash tool uses a function only available
  109.     # in 10.3 and later.
  110.     IsPre10_3
  111.     if [ $? -ne 0 ]
  112.     then
  113.         REMOVE_ACTION="remove"
  114.     fi
  115.     
  116.     # Locate rm
  117.     RM="/bin/rm -rf"
  118.     
  119.     # Define the trash dir
  120.     TRASH_DIR="~/.Trash"
  121.     
  122. #     echo "TRASH_DIR=${TRASH_DIR}"
  123.  
  124.     # Make sure the trash directory is writable
  125.     # TODO - why does this fail?!
  126. #    if ! [ -d "${TRASH_DIR}" ]
  127. #    then
  128. #        echo "'${TRASH_DIR}' is not a directory"
  129. #        # exit 1
  130. #    fi
  131. #    if ! [ -w "${TRASH_DIR}" ]
  132. #    then
  133. #        echo "'${TRASH_DIR}' is not writable"
  134. #        # exit 1
  135. #    fi
  136.  
  137.     KILL_DEVICE_MONITOR="${REMOVE_BIN}/KillDeviceMonitor"
  138.     KILL_SESSION_MONITOR="${REMOVE_BIN}/KillSessionMonitor"
  139.     COMPONENT_TOOL="${REMOVE_BIN}/ComponentTool"
  140.     
  141. #    validateTool "${KILL_DEVICE_MONITOR}"
  142. #    validateTool "${KILL_SESSION_MONITOR}"
  143. #    validateTool "${COMPONENT_TOOL}"
  144.     
  145. }
  146.  
  147. # ----------------------------------------------------------------------------
  148. # defineInstallLocations
  149. #     expects VOLUME to be defined
  150. #
  151. #    defines the following directories
  152. #        APPLICATION_SUPPORT_DIR
  153. #        APPLICATION_SUPPORT_BIN_DIR
  154. #        APPLICATION_DIR
  155. #        COMPONENTS_DIR
  156. #        SYSTEM_EXTENSIONS_DIR
  157. #        STARTUP_ITEMS
  158. #        SYSTEM_STARTUP_ITEMS
  159. #        PREFERENCES_DIR
  160. #        HOME_PREFERENCES_DIR
  161. #
  162.  
  163. function defineInstallLocations()
  164. {
  165.     VENDOR_SUBDIR="$1"
  166.     VENDOR_APP_SUBDIR="$2"
  167.     APPLICATION_SUPPORT_DIR="${VOLUME}/Library/Application Support/${VENDOR_SUBDIR}"
  168.     APPLICATION_SUPPORT_BIN_DIR="${APPLICATION_SUPPORT_DIR}/Private"
  169.     APPLICATION_SUPPORT_BIN_DIR_OLD="${APPLICATION_SUPPORT_DIR}/bin"
  170.     APPLICATION_SUPPORT_BIN_DIR_OLD_2="${APPLICATION_SUPPORT_DIR}/Applications"
  171.     APPLICATION_DIR="${VOLUME}/Applications/${VENDOR_APP_SUBDIR}"
  172.     COMPONENTS_DIR="${VOLUME}/Library/Components"
  173.     SYSTEM_EXTENSIONS_DIR="${VOLUME}/System/Extensions"
  174.     STARTUP_ITEMS="${VOLUME}/Library/StartupItems"
  175.     SYSTEM_STARTUP_ITEMS="${VOLUME}/System/Library/StartupItems"
  176.     PREFERENCES_DIR="${VOLUME}/Library/Preferences"
  177.     HOME_PREFERENCES_DIR="~/Library/Preferences"
  178.     RECEIPTS_DIR="${VOLUME}/Library/Receipts"
  179.     IMAGE_CAPTURE_DIR="${VOLUME}/System/Library/Image Capture/Devices"
  180. }
  181.  
  182. # ----------------------------------------------------------------------------
  183. function moveToTrash()
  184. {
  185.     if ! [ -z "${TRASH_TOOL}" ]
  186.     then
  187.         if [ "${REMOVE_SHOW_ONLY}" != 0 ]
  188.         then
  189.             echo "${TRASH_TOOL}" "$1"
  190.         else
  191.             "${TRASH_TOOL}" "$1"
  192.         fi
  193.         return 0
  194.     fi
  195.     return 1
  196. }
  197.  
  198. # ----------------------------------------------------------------------------
  199. function mvMacToTrash()
  200. {
  201.     if ! [ -z "${MVMAC}" ]
  202.     then
  203.         if [ "${REMOVE_SHOW_ONLY}" != 0 ]
  204.         then
  205.             echo "${MVMAC}" "$1" "${TRASH_DIR}"
  206.         else
  207.             "${MVMAC}" "$1" "${TRASH_DIR}"
  208.         fi
  209.         return 0
  210.     fi
  211.     return 1
  212. }
  213.  
  214. # ----------------------------------------------------------------------------
  215. function remove()
  216. {
  217.     if [ "${REMOVE_SHOW_ONLY}" != 0 ]
  218.     then
  219.         echo ${RM} "$1"
  220.     else
  221.         ${RM} "$1"
  222.     fi
  223.     return 0
  224. }
  225.  
  226. # ----------------------------------------------------------------------------
  227. function removeAction()
  228. {
  229.     if [ -e "$1" ]
  230.     then
  231.         if [ "${REMOVE_ACTION}" == "trash" ]
  232.         then
  233.             moveToTrash "$1"
  234.             if test $? -ne 0
  235.             then
  236.                 mvMacToTrash "$1"
  237.             fi
  238.         
  239.         elif [ "${REMOVE_ACTION}" == "mvmac" ]
  240.         then
  241.             mvMacToTrash "$1"
  242.         
  243.         elif [ "${REMOVE_ACTION}" == "remove" ]
  244.         then
  245.             remove "$1"
  246.         
  247.         elif [ "${REMOVE_ACTION}" == "show" ]
  248.         then
  249.             echo "$1"
  250.         
  251.         else
  252.             echo "Unknown action in remove function"
  253.             exit 1
  254.         fi
  255.         
  256.         if test $? -ne 0
  257.         then
  258.             echo "${REMOVE_ACTION} failed for $1"
  259.         fi
  260.         
  261.     fi
  262. }
  263.  
  264. # ----------------------------------------------------------------------------
  265. function removeActionVariants()
  266. {
  267.     removeAction "$1$2"
  268.     removeAction "$1-Debug$2"
  269.     removeAction "$1-Logging$2"
  270. }
  271.  
  272. # ----------------------------------------------------------------------------
  273. function removeActionGlob()
  274. {
  275.     local FILES=`ls -1 $1 2>/dev/null`
  276.     
  277.     for name in ${FILES}
  278.     do
  279.         # echo "FOUND: ${name}"
  280.         removeAction "${name}"
  281.     done
  282. }
  283.  
  284. # ----------------------------------------------------------------------------
  285. function removeActionCustomizedComponents()
  286. {
  287.     local FILES=`"${COMPONENT_TOOL}" -showcustomized "$1" "$2" "$3" "$4" "$5" "$6"`
  288.     
  289.     for name in ${FILES}
  290.     do
  291.         # echo "FOUND: ${name}"
  292.         removeAction "${name}"
  293.     done
  294. }
  295.  
  296.  
  297. # ----------------------------------------------------------------------------
  298. function killDeviceMonitor()
  299. {
  300.  
  301.     if ! [ -z "${KILL_DEVICE_MONITOR}" ]
  302.     then
  303.         if [[ "${REMOVE_ACTION}" == "trash"  || "${REMOVE_ACTION}" == "mvmac" ||  "${REMOVE_ACTION}" == "remove" ]]
  304.         then
  305.             if [ "${REMOVE_SHOW_ONLY}" != 0 ]
  306.             then
  307.                 echo "'${KILL_DEVICE_MONITOR}'"
  308.             else
  309.                 echo "Stopping Device Monitor..."
  310.                 "${KILL_DEVICE_MONITOR}" "$1"
  311.                 local rv=$?
  312.                 if test "${rv}" -gt 1; then
  313.                     bailOnError ${rv} "${KILL_DEVICE_MONITOR} failed"
  314.                 fi
  315.             fi
  316.         
  317.         elif [[ "${REMOVE_ACTION}" == "show" ]]
  318.         then
  319.             echo "'${KILL_DEVICE_MONITOR}'"
  320.  
  321.         else
  322.             echo "Unknown action in killDeviceMonitor function"
  323.             exit 1
  324.         fi
  325.     else
  326.         echo "   TODO - try killall to kill device monitor" > /dev/stderr
  327.     fi
  328. }
  329.  
  330. # ----------------------------------------------------------------------------
  331. function killSessionMonitor()
  332. {
  333.     
  334.     if ! [ -z "${KILL_SESSION_MONITOR}" ]
  335.     then
  336.         if [[ "${REMOVE_ACTION}" == "trash"  || "${REMOVE_ACTION}" == "mvmac" ||  "${REMOVE_ACTION}" == "remove" ]]
  337.         then
  338.             if [ "${REMOVE_SHOW_ONLY}" != 0 ]
  339.             then
  340.                 echo "'${KILL_SESSION_MONITOR}'"
  341.             else
  342.                 echo "Stopping Session Monitor..."
  343.                 "${KILL_SESSION_MONITOR}"
  344.                 local rv=$?
  345.                 if test "${rv}" -gt 1; then
  346.                     bailOnError ${rv} "${KILL_SESSION_MONITOR} failed"
  347.                 fi
  348.             fi
  349.         
  350.         elif [[ "${REMOVE_ACTION}" == "show" ]]
  351.         then
  352.             echo "'${KILL_SESSION_MONITOR}'"
  353.  
  354.         else
  355.             echo "Unknown action in killSessionMonitor function"
  356.             exit 1
  357.         fi
  358.     else
  359.         echo "   TODO - try killall to killSessionMonitor" > /dev/stderr
  360.     fi
  361. }
  362.  
  363.  
  364. # ----------------------------------------------------------------------------
  365. function killRegisterApp()
  366. {
  367.     echo "    TODO - stop Register App..." > /dev/stderr
  368.     # 
  369.     # if ! [ -z "${KILL_SESSION_MONITOR}" ]
  370.     # then
  371.     #         "${KILL_SESSION_MONITOR}"
  372.     #        local rv=$?
  373.     #        if test "${rv}" -gt 1; then
  374.     #            bailOnError ${rv} "${KILL_SESSION_MONITOR} failed"
  375.     #        fi
  376.     #else
  377.     #    echo "   TODO - try killall to kill device monitor" > /dev/stderr
  378.     #fi
  379. }
  380.  
  381. # ----------------------------------------------------------------------------
  382. # 0 if not dir or not empty
  383. # 1 if empty
  384.  
  385. function isDirEmpty()
  386. {
  387.     # echo "isDirEmpty($1)"
  388.     if [ -z "$1" ]
  389.     then
  390.         return 0
  391.     fi
  392.  
  393.     if [ -d "$1" ]
  394.     then
  395.         local count=0
  396.         local items=`ls -1A "$1" | sed -e 's/.DS_Store//'`
  397.         # echo "isDirEmpty($1) - contents = '${items[@]}'"
  398.         for name in  "${items[@]}"
  399.         do
  400.             # echo "CONTENTS-${name}-"
  401.             if ! [ -z "${name}" ]
  402.             then
  403.                 let count=count+1
  404.             fi
  405.         done
  406.         # echo COUNT=${count}
  407.         if test ${count} -eq 0
  408.         then
  409.             return 1
  410.         fi
  411.     fi
  412.     return 0
  413. }
  414.  
  415. # ----------------------------------------------------------------------------
  416. # rm -rf "$1" if an empty directory (.DS_Store allowed)
  417.  
  418. function removeEmptyDir()
  419. {
  420.     # set -x
  421.     isDirEmpty "$1"
  422.     if test $? -ne 0
  423.     then
  424.         echo "Removing empty directory $1"
  425.         removeAction "$1"
  426.     fi
  427.     # set +x
  428. }
  429.  
  430. # ----------------------------------------------------------------------------
  431. # 0 if not dir or not empty
  432.  
  433. function countDir()
  434. {
  435.     local count=0
  436.     if [ -d "$1" ]
  437.     then
  438.         local items=`ls -1A "$1" | sed -e 's/.DS_Store//'`
  439.         for name in  "${items[@]}"
  440.         do
  441.             let count=count+1
  442.         done
  443.     fi
  444.     return ${count}
  445. }
  446.  
  447. # ----------------------------------------------------------------------------
  448. # returns nonzero if receipt found
  449.  
  450. function checkForReceipt()
  451. {
  452.     local items=`ls -1A "${RECEIPTS_DIR}/$1.pkg" 2>/dev/stderr`
  453.     local count=0
  454.     for name in  "${items[@]}"
  455.     do
  456.         let count=count+1
  457.     done
  458.     return ${count}
  459. }
  460.  
  461.  
  462. # ----------------------------------------------------------------------------
  463. # Determine whether we are 10.2 and earlier or not
  464. # returns nonzero if pre 10.3
  465.  
  466. function IsPre10_3()
  467. {
  468.     local PRE_10_3=1
  469.     if [ -x "/usr/bin/sw_vers" ]
  470.     then
  471.         local _V=`/usr/bin/sw_vers | grep ProductVersion | sed -e 's/ProductVersion:[^0-9]*//'`
  472.         local maj=`echo ${_V} | sed -e 's/\..*//'`
  473.         local min=`echo ${_V} | sed -e 's/[0-9]*\.//' -e 's/\..*//'`
  474.         # echo V ${_V}
  475.         # echo "MAJ '${maj}'"
  476.         # echo "MIN '${min}'"
  477.  
  478.         if  [ ${maj} -gt 10 ]
  479.         then
  480.             PRE_10_3=0
  481.         elif [[ ${maj} -eq 10 && ${min} -gt 2 ]]
  482.         then
  483.             PRE_10_3=0
  484.         fi
  485.     fi
  486.     return ${PRE_10_3}
  487. }
  488.  
  489. # ----------------------------------------------------------------------------
  490. # Test whether OS is prior to any specified version
  491.  
  492. function IsOSBefore()
  493. {
  494.     local testMajor=$1
  495.     local testMinor=$2
  496.     local testPoint=$3
  497.     
  498.     if [ -z "${testMajor}" ]
  499.     then
  500.         return 0
  501.     fi
  502.     
  503.     if [ -z "${testMinor}" ]
  504.     then
  505.         testMinor=0
  506.     fi
  507.     if [ -z "${testPoint}" ]
  508.     then
  509.         testPoint=0
  510.     fi
  511.     
  512.     local IS_PRE=1
  513.     if [ -x "/usr/bin/sw_vers" ]
  514.     then
  515.         local _V=`/usr/bin/sw_vers | grep ProductVersion | sed -e 's/ProductVersion:[^0-9]*//'`
  516.         local maj=`echo ${_V} | sed -e 's/\..*//'`
  517.         local min=`echo ${_V} | sed -e 's/[0-9]*\.//' -e 's/\..*//'`
  518.         # TODO - extract point version if available, default to 0
  519.         
  520.         # echo V ${_V}
  521.         # echo "MAJ '${maj}'"
  522.         # echo "MIN '${min}'"
  523.         
  524.         # TODO - test against point version if available
  525.  
  526.         if  [ ${maj} -gt ${testMajor} ]
  527.         then
  528.             IS_PRE=0
  529.         elif [[ ${maj} -eq ${testMajor} && ${min} -ge ${testMinor} ]]
  530.         then
  531.             IS_PRE=0
  532.         fi
  533.     fi
  534.     return ${IS_PRE}
  535. }
  536.